import pygame
import random
import game_object

class Cloud(game_object.GameObject):
    WIDTH = 50
    HEIGHT = 40
    IMAGES = []
    ANIMATION_DURATION = 0.2
    
    def __init__(self, screen_width, screen_height):
        game_object.GameObject.__init__(self, screen_width, screen_height)
        if not len(Cloud.IMAGES):
            Cloud.IMAGES = game_object.slice_sprites(Cloud.WIDTH, Cloud.HEIGHT, "../res/images/cloud.png")

        self.x = random.randint(0, game_object.GameObject.SCREEN_WIDTH - Cloud.WIDTH)
        self.y = random.randint(0, game_object.GameObject.SCREEN_HEIGHT / 2 - Cloud.HEIGHT)
        self.change()

        self.state = 0
        self.animation_time = 0.0
        self.image = Cloud.IMAGES[self.state]
        self.rect = pygame.Rect(self.x, self.y, Cloud.WIDTH, Cloud.HEIGHT)

    def update(self, time):
        self.animation_time += time
        if self.animation_time > len(Cloud.IMAGES) * Cloud.ANIMATION_DURATION:
            self.animation_time = 0.0
        self.state = int(self.animation_time / Cloud.ANIMATION_DURATION)
        self.image = Cloud.IMAGES[self.state]
        
        self.x += self.xVel * time * 100
        self.y += self.yVel * time * 100
        self.rect = pygame.Rect(self.x, self.y, Cloud.WIDTH, Cloud.HEIGHT)
        self.wall_collision()

    def wall_collision(self):
        if self.x < 0:
            self.x = 0
            self.change()
        elif self.x > game_object.GameObject.SCREEN_WIDTH - Cloud.WIDTH:
            self.x = game_object.GameObject.SCREEN_WIDTH - Cloud.WIDTH
            self.change()
        if self.y < 0:
            self.y = 0
            self.change()
        elif self.y > game_object.GameObject.SCREEN_HEIGHT - Cloud.HEIGHT:
            self.y = game_object.GameObject.SCREEN_HEIGHT - Cloud.HEIGHT
            self.change()
            
    def change(self):
        self.xVel = random.randint(-4,4)
        self.yVel = random.randint(-4,4)
        if(self.xVel == 0 and self.yVel == 0):
            self.change()
